﻿// CngRSATest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <bcrypt.h>

#define NT_SUCCESS(Status)          (((NTSTATUS)(Status)) >= 0)
#define STATUS_UNSUCCESSFUL         ((NTSTATUS)0xC0000001L)

#define PrivateKeySize 283
#define PublicKeySize 155
#define InputDataSize 128

PUCHAR encryptedBuffer;
ULONG encryptedBufferSize = 128;


VOID printMem(PVOID Mem, int length)
{
    int i;
    for (i = 0; i < length; i++)
        printf("%02x ", ((unsigned char *)Mem)[i]);
}

VOID Decrypt()
{
    //unsigned char PrivateKey[PrivateKeySize] = { 0x52, 0x53, 0x41, 0x32, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xB7, 0x50, 0x52, 0xDD, 0x58, 0xE4, 0x96, 0xAF, 0x91, 0xE5, 0xB2, 0x7B, 0x0A, 0xE6, 0xAA, 0x1F, 0x71, 0x8A, 0x66, 0xC3, 0xF0, 0x21, 0xD8, 0xE6, 0x2C, 0xD6, 0x25, 0x2E, 0x77, 0x3C, 0x61, 0x08, 0x1B, 0x69, 0xE7, 0x58, 0xDF, 0x3B, 0x07, 0xFE, 0xF1, 0xDB, 0xBF, 0xA6, 0x35, 0xDF, 0xC7, 0x49, 0x06, 0xC8, 0xDB, 0x74, 0x2A, 0xB9, 0xED, 0xB3, 0x04, 0x80, 0x75, 0x5F, 0x71, 0x2C, 0xD0, 0x14, 0x0E, 0x81, 0x18, 0x00, 0x5E, 0x34, 0x5A, 0xC2, 0x3A, 0x84, 0x63, 0xB1, 0x6B, 0x04, 0x21, 0x49, 0x7F, 0xE0, 0xF3, 0x52, 0x5E, 0x61, 0x43, 0xB1, 0x8F, 0x7C, 0xF2, 0x74, 0x29, 0x28, 0x69, 0x20, 0x36, 0xC0, 0x92, 0x17, 0x42, 0x99, 0x72, 0xE5, 0xE7, 0x82, 0xBE, 0x8E, 0x3B, 0x3F, 0xC9, 0x0A, 0xE1, 0xC4, 0x63, 0x68, 0x73, 0x1D, 0x67, 0x8D, 0xC0, 0xA3, 0xB4, 0xBA, 0xF0, 0xB7, 0xB0, 0x9B, 0xBB, 0x3F, 0xB8, 0x6E, 0xC0, 0x34, 0x1E, 0xA0, 0x01, 0x4B, 0x6D, 0x47, 0x73, 0x3F, 0xA5, 0x39, 0x05, 0x27, 0xD4, 0xD1, 0x38, 0x34, 0x32, 0x2C, 0x5B, 0x03, 0x5F, 0x16, 0x21, 0x64, 0x04, 0xD5, 0x19, 0xDB, 0xE7, 0x80, 0xDA, 0xBD, 0xC4, 0x1E, 0xAB, 0x61, 0xC8, 0x84, 0xDF, 0x54, 0x16, 0x77, 0x98, 0x9B, 0x90, 0x03, 0x83, 0xC4, 0x8D, 0x25, 0xB1, 0x32, 0x67, 0x77, 0x6A, 0x1C, 0x64, 0x2D, 0xFA, 0x9E, 0xB9, 0x26, 0xB5, 0xF8, 0x47, 0x4A, 0x9C, 0x35, 0x89, 0x5F, 0x12, 0x0E, 0xFF, 0x60, 0x87, 0x1E, 0x27, 0xC1, 0xC5, 0x7C, 0x77, 0x0A, 0xAE, 0x11, 0x37, 0xE3, 0x42, 0x9B, 0xAF, 0x9D, 0xBC, 0xC2, 0x52, 0xF8, 0x85, 0xBA, 0xED, 0x8E, 0xC3, 0x73, 0x04, 0x0A, 0x53, 0xD2, 0x1D, 0xEF, 0xA0, 0x6A, 0xCD, 0xBE, 0x93, 0x49, 0x34, 0x3A, 0xBD, 0xDF, 0x6A, 0x33, 0x25, 0x91, 0xFC, 0xE7 };
    // 1024bit
    unsigned char PrivateKey[PrivateKeySize] = { 0x52, 0x53, 0x41, 0x32, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x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x60, 0xed, 0x45, 0x7c, 0xc4, 0x19, 0xd6, 0x98, 0x35, 0x44, 0xfa, 0x88, 0x60, 0x89, 0xf7, 0x26, 0x3b, 0xed, 0x1d, 0x89, 0x50, 0x56, 0xb1, 0xa9, 0xfe, 0x99, 0xb3, 0xac, 0xb7, 0xda, 0xca, 0x9b, 0x71, 0x88, 0xce, 0x24, 0x63, 0x19, 0xef, 0x49, 0x26, 0xac, 0xbb, 0x3e, 0x6e, 0x95, 0x59, 0x17, 0x72, 0x0b, 0x48, 0xab, 0x30, 0x85, 0x27, 0xff, 0xff, 0x05, 0x78, 0x03, 0x21, 0x77, 0x4f, 0x0d, 0xe5, 0xdf, 0xc8, 0x36, 0x99, 0xfb, 0x9d, 0x0a, 0x88, 0x94, 0xbb, 0xc3, 0x31, 0x6e, 0x2c, 0x69, 0x0f };
    BCRYPT_ALG_HANDLE hAlgorithm = NULL;
    BCRYPT_KEY_HANDLE hKey = NULL;
    ULONG plaintextSize = 128;
    PUCHAR decryptedBuffer = NULL;
    ULONG decryptedBufferSize;
    NTSTATUS status;

    status = BCryptOpenAlgorithmProvider(&hAlgorithm,
        BCRYPT_RSA_ALGORITHM,
        NULL,
        0);
    if (!NT_SUCCESS(status)) {
        printf("Failed to get algorithm provider..status : %08x\n", status);
        goto cleanup;
    }

    status = BCryptImportKeyPair(hAlgorithm,
        NULL,
        BCRYPT_RSAPRIVATE_BLOB,
        &hKey,
        PrivateKey,
        PrivateKeySize,
        BCRYPT_NO_KEY_VALIDATION);
    if (!NT_SUCCESS(status)) {
        printf("Failed to import Private key..status : %08x\n", status);
        goto cleanup;
    }

    status = BCryptDecrypt(hKey,
        encryptedBuffer,
        encryptedBufferSize,
        NULL,
        NULL,
        0,
        NULL,
        0,
        &decryptedBufferSize,
        0);
    if (!NT_SUCCESS(status)) {
        printf("Failed to get required size of buffer..status : %08x\n", status);
        goto cleanup;
    }

    decryptedBuffer = (PUCHAR)HeapAlloc(GetProcessHeap(), 0, decryptedBufferSize);
    if (decryptedBuffer == NULL) {
        printf("failed to allocate memory for buffer\n");
        goto cleanup;
    }

    status = BCryptDecrypt(hKey,
        encryptedBuffer,
        encryptedBufferSize,
        NULL,
        NULL,
        0,
        decryptedBuffer,
        decryptedBufferSize,
        &decryptedBufferSize,
        0);
    if (!NT_SUCCESS(status)) {
        printf("Failed decrypt buffer..status : %08x\n", status);
        goto cleanup;
    }

    printf("Decrypted buffer\n");
    printMem(decryptedBuffer, decryptedBufferSize);
    printf("\n\n");

cleanup:
    if(decryptedBuffer)
        HeapFree(GetProcessHeap(), 0, decryptedBuffer);

    BCryptDestroyKey(hKey);

    BCryptCloseAlgorithmProvider(hAlgorithm, 0);
}


VOID Encrypt()
{
    //unsigned char PublicKey[PublicKeySize] = { 0x52, 0x53, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,  0x01, 0x00, 0x01, 0xB7,  0x50, 0x52, 0xDD, 0x58, 0xE4, 0x96, 0xAF, 0x91,  0xE5, 0xB2, 0x7B, 0x0A,  0xE6, 0xAA, 0x1F, 0x71,  0x8A, 0x66, 0xC3, 0xF0, 0x21, 0xD8, 0xE6, 0x2C,  0xD6, 0x25, 0x2E, 0x77,  0x3C, 0x61, 0x08, 0x1B,  0x69, 0xE7, 0x58, 0xDF, 0x3B, 0x07, 0xFE, 0xF1,  0xDB, 0xBF, 0xA6, 0x35,  0xDF, 0xC7, 0x49, 0x06,  0xC8, 0xDB, 0x74, 0x2A, 0xB9, 0xED, 0xB3, 0x04,  0x80, 0x75, 0x5F, 0x71,  0x2C, 0xD0, 0x14, 0x0E,  0x81, 0x18, 0x00, 0x5E, 0x34, 0x5A, 0xC2, 0x3A,  0x84, 0x63, 0xB1, 0x6B,  0x04, 0x21, 0x49, 0x7F,  0xE0, 0xF3, 0x52, 0x5E, 0x61, 0x43, 0xB1, 0x8F,  0x7C, 0xF2, 0x74, 0x29,  0x28, 0x69, 0x20, 0x36,  0xC0, 0x92, 0x17, 0x42, 0x99, 0x72, 0xE5, 0xE7,  0x82, 0xBE, 0x8E, 0x3B,  0x3F, 0xC9, 0x0A, 0xE1,  0xC4, 0x63, 0x68, 0x73, 0x1D, 0x67, 0x8D, 0xC0,  0xA3, 0xB4, 0xBA, 0xF0,  0xB7, 0xB0, 0x9B };
    // 1024bit
    unsigned char PublicKey[PublicKeySize] = { 0x52, 0x53, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xb5, 0x4e, 0xbb, 0x6f, 0x7a, 0xb0, 0xff, 0xf0, 0x7c, 0x6a, 0xda, 0x0e, 0xfb, 0xd3, 0xc9, 0x02, 0x36, 0xf4, 0x6e, 0xe4, 0x86, 0x33, 0x24, 0x36, 0xc6, 0x8c, 0x02, 0x32, 0x74, 0x12, 0xd7, 0xe7, 0xd4, 0x1b, 0x70, 0xfc, 0x50, 0xb8, 0x7c, 0xbb, 0x2a, 0x52, 0x3d, 0x29, 0x2b, 0x74, 0x0e, 0x96, 0x96, 0xf5, 0xa1, 0x93, 0xf7, 0xed, 0xab, 0xd9, 0x19, 0x76, 0x3a, 0xf4, 0x9b, 0x05, 0x1c, 0x4a, 0x8b, 0xd9, 0x30, 0xe3, 0x6b, 0xfc, 0xf9, 0xaf, 0x8a, 0x87, 0x70, 0x1b, 0x27, 0x00, 0xf6, 0xd3, 0xf2, 0xf0, 0x73, 0x84, 0xc8, 0x47, 0x5f, 0x5d, 0xbf, 0x08, 0x16, 0xf6, 0xfc, 0xc4, 0xaa, 0x4b, 0x70, 0xf0, 0xaf, 0xec, 0x6c, 0x34, 0x7c, 0xeb, 0xd8, 0x86, 0x86, 0x91, 0x16, 0x7c, 0x0a, 0xd6, 0x11, 0x9f, 0xc6, 0xf4, 0x4a, 0xe3, 0x30, 0x9f, 0x6c, 0x03, 0xc1, 0x71, 0x60, 0x4d, 0x70, 0x75 };
    unsigned char InputData[InputDataSize];
    BCRYPT_ALG_HANDLE hAlgorithm = NULL;
    BCRYPT_KEY_HANDLE hKey = NULL;
    NTSTATUS status;

    for (int i = 0; i < 128; i++)
        InputData[i] = (unsigned char)rand();

    printf("Random Data is \n");
    printMem(InputData, InputDataSize);
    printf("\n\n");

    status = BCryptOpenAlgorithmProvider(&hAlgorithm,
        BCRYPT_RSA_ALGORITHM,
        NULL,
        0);
    if (!NT_SUCCESS(status)) {
        printf("Failed to get algorithm provider..status : %08x\n", status);
        goto cleanup;
    }

    status = BCryptImportKeyPair(hAlgorithm,
        NULL,
        BCRYPT_RSAPUBLIC_BLOB,
        &hKey,
        PublicKey,
        PublicKeySize,
        BCRYPT_NO_KEY_VALIDATION);
    if (!NT_SUCCESS(status)) {
        printf("Failed to import Private key..status : %08x\n", status);
        goto cleanup;

    }

    status = BCryptEncrypt(hKey,
        InputData,
        InputDataSize,
        NULL,
        NULL,
        0,
        NULL,
        0,
        &encryptedBufferSize,
        0
    );
    if (!NT_SUCCESS(status)) {
        printf("Failed to get required size of buffer..status : %08x\n", status);
        goto cleanup;
    }

    encryptedBuffer = (PUCHAR)HeapAlloc(GetProcessHeap(), 0, encryptedBufferSize);

    if (encryptedBuffer == NULL) {
        printf("failed to allocate memory for blindedFEKBuffer\n");
        goto cleanup;
    }

    status = BCryptEncrypt(hKey,
        InputData,
        InputDataSize,
        NULL,
        NULL,
        0,
        encryptedBuffer,
        encryptedBufferSize,
        &encryptedBufferSize,
        0
    );

    if (!NT_SUCCESS(status)) {
        printf("Failed encrypt data..status : %08x\n", status);
        goto cleanup;
    }

    printf("Encrypted Data\n");
    printMem(encryptedBuffer, encryptedBufferSize);
    printf("\n\n");

cleanup:
    if (hKey)
        BCryptDestroyKey(hKey);
    if (hAlgorithm)
        BCryptCloseAlgorithmProvider(hAlgorithm, 0);
}

int main()
{
    int i;
    for (i = 0; i < 10; i++)
    {
        Encrypt();
        Decrypt();
    }
    getchar();
}


